package com.example.tests;

import com.example.fw.ApplicationManager;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

import static com.example.tests.AddressDataGenerator.generateRandomAddresses;
import static com.example.tests.AddressDataGenerator.loadAddressesFromCSV;
import static com.example.tests.AddressDataGenerator.loadAddressesFromXML;
import static com.example.tests.GroupDataGenerator.generateRandomGroups;
import static com.example.tests.GroupDataGenerator.loadGroupsFromCSV;
import static com.example.tests.GroupDataGenerator.loadGroupsFromXML;

/**
 * Created with IntelliJ IDEA.
 * User: pshevchenko
 * Date: 06.03.13
 * Time: 11:45
 * To change this template use File | Settings | File Templates.
 */
public class TestBase {



    protected static ApplicationManager applicationManager;
    private int checkCounter;
    private int checkFreq;

    protected static List<Object[]> wrapGroupListFromProvider(List<GroupData> groupList) {
        List<Object[]> result = new ArrayList<Object[]>();
        for (GroupData groupData : groupList){
            result.add(new Object[]{groupData});
        }
        return result;
    }

    protected static List<Object[]> wrapAddressListFromProvider(List<AddressData> addressList) {
        List<Object[]> result = new ArrayList<Object[]>();
        for (AddressData addressData : addressList){
            result.add(new Object[]{addressData});
        }
        return result;
    }

    protected boolean shouldCheck(){
        if (checkCounter++ >= checkFreq){
            checkCounter = 0;
            return true;
        }
        return false;
    }

//----------------------------------------------------------------------------------------------------------------------

    @BeforeTest
    public void setUp() throws Exception {
        String configFileName = System.getProperty("configFile", "application.properties");
        Properties properties = new Properties();
        properties.load(new FileReader(new File(configFileName)));
        applicationManager = new ApplicationManager(properties);
        applicationManager.navigateTo()
                .mainPage();
        checkCounter = 0;
        checkFreq = Integer.parseInt(properties.getProperty("check.freq", "0"));
    }

    @AfterTest
    public void tearDown() throws Exception {
        applicationManager.decompose();
    }

//Group data providers--------------------------------------------------------------------------------------------

    @DataProvider
    public Iterator<Object[]> randomValidGroupDataGenerator(){
        int iterationsCount = 5;
        return wrapGroupListFromProvider(generateRandomGroups(iterationsCount)).iterator();
    }

    @DataProvider
    public Iterator<Object[]> groupsFromXMLFile() throws IOException {
        return wrapGroupListFromProvider(loadGroupsFromXML(new File("groups.xml"))).iterator();
    }

    @DataProvider
    public Iterator<Object[]> groupsFromCSVFile() throws IOException {
        return wrapGroupListFromProvider(loadGroupsFromCSV(new File("groups.xml"))).iterator();
    }

//Address data providers------------------------------------------------------------------------------------------------

    @DataProvider
    public Iterator<Object[]> randomValidAddressDataGenerator(){
        int iterationsCount = 5;
        return wrapAddressListFromProvider(generateRandomAddresses(iterationsCount)).iterator();
    }

    @DataProvider
    public Iterator<Object[]> addressesFromCSVFile() throws IOException{
        return wrapAddressListFromProvider(loadAddressesFromCSV(new File("addresses.csv"))).iterator();
    }

    @DataProvider
    public Iterator<Object[]> addressesFromXMLFile() throws IOException{
        return wrapAddressListFromProvider(loadAddressesFromXML(new File("addresses.xml"))).iterator();
    }
}
